package com.struts2.example.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.opensymphony.xwork2.ActionSupport;
import com.struts2.example.model.Connect;

public class FinishAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String TAG="FinishAction:";
	private int resultIdBean;
	private int numQuestion=0;
	private String[] choiceAnswer ;
	private int count=0;
	private String userNameBean;
    private float percent;

	public String execute()	
	{
		for(int i=0;i<getChoiceAnswer().length;i++)
		{
			System.out.print(TAG+"Choice answer :"+getChoiceAnswer()[i]+"\n");
		}	
		String ret = ERROR;
		Connect conn = new Connect();
		Connection connected = conn.GetConnect();
		
		try {
			String sql = "SELECT * FROM testonline_bang_dai.result_temple WHERE result_id=? ";
			PreparedStatement ps = connected.prepareStatement(sql);
			ps.setInt(1,resultIdBean);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				boolean wrongAnswer=false;
				numQuestion++;
				sql="SELECT * FROM testonline_bang_dai.answer WHERE question_id= ? and type_answer = 1";
				ps = connected.prepareStatement(sql);
				ps.setInt(1,rs.getInt(2));
				ResultSet  rsSub = ps.executeQuery();
			
				while(rsSub.next())
				{
					System.out.print(TAG+"answer : "+rsSub.getString(3)+"\n");
					for(int i=0;i<getChoiceAnswer().length;i++)
					{
						if(getChoiceAnswer()[i].equals(rsSub.getString(3)))
						{
							count++;
						}
					}
				}
				sql="SELECT * FROM testonline_bang_dai.answer WHERE question_id= ? and type_answer = 0";
				ps = connected.prepareStatement(sql);
				ps.setInt(1,rs.getInt(2));
				ResultSet  rsWrongAnswer = ps.executeQuery();	
				while(rsWrongAnswer.next())
				{
					
					for(int i=0;i<getChoiceAnswer().length;i++)
					{
						if(getChoiceAnswer()[i].equals(rsWrongAnswer.getString(3)))
						{
							wrongAnswer=true;
						}
					}

				}
				if(wrongAnswer)
				{
					count--;
				}
				
				ret = SUCCESS;
			}
            percent=((float)getCount()/(float)getNumQuestion())*100;
            System.out.print(TAG+"percent :"+percent+"numQuestion"+numQuestion+"count"+count);
		} catch (Exception e) {
			ret = ERROR;
		} finally {
			if (connected != null) {
				try {
					connected.close();
				} catch (Exception e) {
				}
			}
		}
		return ret;
	}
	
	public float getPercent() {
		return percent;
	}

	public void setPercent(float percent) {
		this.percent = percent;
	}

	public String[] getChoiceAnswer() {
		return choiceAnswer;
	}

	public void setChoiceAnswer(String[] choiceAnswer) {
		this.choiceAnswer = choiceAnswer;
	}

	public int getResultIdBean() {
		return resultIdBean;
	}

	public void setResultIdBean(int resultIdBean) {
		this.resultIdBean = resultIdBean;
	}

	public int getNumQuestion() {
		return numQuestion;
	}

	public void setNumQuestion(int numQuestion) {
		this.numQuestion = numQuestion;
	}
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getUserNameBean() {
		return userNameBean;
	}

	public void setUserNameBean(String userNameBean) {
		this.userNameBean = userNameBean;
	}


}
